//
//  Triangle.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/15.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "Triangle.h"

@interface Triangle ()
@property (weak, nonatomic) IBOutlet UIView *traingleView;

@end

#define SideLengthOfTriangle (self.traingleView.bounds.size.width)//三角形的边长
#define PointToCenterLength (SideLengthOfTriangle/3 *sqrt(3))//三角形的的点到中心点的距离
#define TriangleCenterPoint CGPointMake(SideLengthOfTriangle/2,PointToCenterLength)//三角形的中心点
#define ConVertCenterPoint [self.traingleView convertPoint:TriangleCenterPoint toView:self] //由三角形的坐标点转变成整个view的坐标点

@implementation Triangle

- (void)awakeFromNib
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 10;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect

{
    
    //设置背景颜色
    
    [[UIColor whiteColor]set];
    
    UIRectFill([self bounds]);
    
    CGPoint pointTrianglePeak1 = CGPointMake(ConVertCenterPoint.x, ConVertCenterPoint.y - PointToCenterLength * self.shoot);
    CGPoint pointTrianglePeak2 = CGPointMake(ConVertCenterPoint.x - cos(M_PI/6) * PointToCenterLength * self.vatility, ConVertCenterPoint.y + sin(M_PI/6) * PointToCenterLength * self.vatility);
    CGPoint pointTrianglePeak3 = CGPointMake(ConVertCenterPoint.x + cos(M_PI/6) * PointToCenterLength * self.strong, ConVertCenterPoint.y + sin(M_PI/6) * PointToCenterLength * self.strong);
    
    [self drawTriangleViewWithPoint1:pointTrianglePeak1 point2:pointTrianglePeak2 point:ConVertCenterPoint fillColor:[UIColor colorWithHex:0x7ec1c3 alpha:1.0] strokeColor:[UIColor colorWithHex:0x7ec1c3 alpha:1.0]];
    [self drawTriangleViewWithPoint1:pointTrianglePeak2 point2:pointTrianglePeak3 point:ConVertCenterPoint fillColor:[UIColor colorWithHex:0x0f989e alpha:1.0] strokeColor:[UIColor colorWithHex:0x0f989e alpha:1.0]];
    [self drawTriangleViewWithPoint1:pointTrianglePeak1 point2:pointTrianglePeak3 point:ConVertCenterPoint fillColor:[UIColor colorWithHex:0x53b1b5 alpha:1.0] strokeColor:[UIColor colorWithHex:0x53b1b5 alpha:1.0]];
    
}

- (void)drawTriangleViewWithPoint1:(CGPoint)point1 point2:(CGPoint)point2 point:(CGPoint)point3 fillColor:(UIColor *)fillColor strokeColor:(UIColor *)strokeColor
{
    
    //拿到当前视图准备好的画板
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //利用path进行绘制三角形
    
    CGContextBeginPath(context);//标记
    
    CGContextMoveToPoint(context,point1.x,point1.y);//设置起点
    
    CGContextAddLineToPoint(context,point2.x, point2.y);
    
    CGContextAddLineToPoint(context,point3.x,point3.y);
    
    CGContextClosePath(context);//路径结束标志，不写默认封闭
    
    [fillColor setFill]; //设置填充色
    
    [strokeColor setStroke]; //设置边框颜色
    
    CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path

}

@end
